/*
 * settings.cpp - application settings storage functions
 * Copyright (C) 2009  Sergei Stolyarov
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * Kajim::ChatInputText widget implements basic functions required
 * by chat text entry widget: history navigation, autocompletion etc
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#include <QtGui>
#include <QtCore>

#include "settings.h"
#include "constants.h"

QSettings * settings = 0;

QMap<QString, QVariant> defaults;

void Kajim::Settings::init(const QString & configDir)
{
    if (settings == 0) {
        QDir dir(configDir);
        settings = new QSettings(dir.filePath("config.ini"), QSettings::IniFormat);

        // init default values
        defaults["accounts/list"] = QStringList();
        defaults["rosterwindow/size"] = QSize(250, 600);
        defaults["rosterwindow/pos"] = QPoint(0, 0);
        defaults["accountsdialog/pos"] = QPoint(100, 100);
        defaults["accountsdialog/size"] = QSize(400, 300);
        defaults["accountsdialog/splitter-state"] = QByteArray();
        defaults["ui/layout"] = Kajim::Ui::LAYOUT_MULTIPLE_WINDOWS;
        defaults["ui/quit_timeout"] = 20; // in seconds

    }
}

void Kajim::Settings::set(const QString & name, const QVariant & value)
{
    if (settings != 0) {
        settings->setValue(name, value);
    }
}

QVariant Kajim::Settings::get(const QString & name)
{
    if (settings != 0) {
        QVariant default_value;

        if (defaults.contains(name)) {
            default_value = defaults[name];
        }
        QVariant value = settings->value(name, default_value);
        // type of value must be the same as default_value (if default_value is defined )
        if (default_value.isValid() && default_value.type() != value.type()) {
            value = default_value;
        }

        return value;
    }

    return QVariant();
}

void Kajim::Settings::remove(const QString & name)
{
    if (settings != 0) {
        settings->remove(name);
    }
}

void Kajim::Settings::sync()
{
    settings->sync();
}


